<?php
/**
 * Array storing MySQL table information for Concerto
 *
 * Better if this wasn't touched. Like really.
 *
 * @author Jason Raede <jason@limitdesigns.com>
 * @package Concerto
 * @subpackage Database
 */

$cn_db_tables = array(
	// CONTENT
	'cn_content' => 
	
'CREATE TABLE `cn_content` (
  `content_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `content_title` varchar(300) NOT NULL,
  `content_status` varchar(20) DEFAULT NULL,
  `content_content` longtext,
  `content_author` bigint(20) DEFAULT NULL,
  `content_permalink` varchar(100) DEFAULT NULL,
  `content_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `content_type` int(3) NOT NULL,
  `content_parent` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`content_id`),
  KEY `content_type` (`content_type`),
  FULLTEXT KEY `fulltext` (`content_title`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;',

	// CONTENT-CONTENT RELATIONS
	'cn_content_content_relations' => 
	
'CREATE TABLE `cn_content_content_relations` (
  `ccr_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `ccr_content1` bigint(20) NOT NULL,
  `ccr_content2` bigint(20) NOT NULL,
  `ccr_content3` bigint(20) NOT NULL,
  `ccr_relation` int(5) NOT NULL,
  `ccr_content1_qualifier` varchar(100) NOT NULL,
  `ccr_content1_quantifier` int(11) NOT NULL,
  `ccr_content2_qualifier` varchar(100) NOT NULL,
  `ccr_content2_quantifier` int(11) NOT NULL,
  `ccr_content3_qualifier` varchar(100) NOT NULL,
  `ccr_content3_quantifier` int(11) NOT NULL,
  PRIMARY KEY (`ccr_id`),
  KEY `ccr_content1` (`ccr_content1`),
  KEY `ccr_content2` (`ccr_content2`),
  KEY `ccr_content3` (`ccr_content3`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;',
	
	// CONTENT FIELDS
	'cn_content_fields' =>
	
'CREATE TABLE `cn_content_fields` (
  `cf_id` int(5) NOT NULL AUTO_INCREMENT,
  `cf_display_name` varchar(50) DEFAULT NULL,
  `cf_data_type` varchar(50) DEFAULT NULL,
  `cf_column` varchar(50) DEFAULT NULL,
  `cf_required` tinyint(1) DEFAULT \'0\',
  `cf_content_type` int(3) NOT NULL,
  `cf_order` int(3) NOT NULL,
  `cf_order_column` int(1) DEFAULT \'1\',
  `cf_show` tinyint(1) DEFAULT NULL,
  `cf_show_login` tinyint(1) DEFAULT \'0\',
  `cf_show_signup` tinyint(1) DEFAULT \'0\',
  `cf_show_edit_profile` tinyint(1) DEFAULT \'0\',
  `cf_description` varchar(500) DEFAULT NULL,
  `cf_aux` varchar(100) DEFAULT NULL,
  `cf_default` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`cf_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',
	
	// AUX DATA FOR CONTENT FIELDS
	'cn_content_field_values' =>

'CREATE TABLE `cn_content_field_values` (
  `cfv_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cfv_content` bigint(20) NOT NULL,
  `cfv_field` bigint(20) NOT NULL,
  `cfv_type` varchar(20) NOT NULL,
  `cfv_text1` varchar(500) NOT NULL,
  `cfv_text2` varchar(500) NOT NULL,
  `cfv_text3` varchar(500) NOT NULL,
  PRIMARY KEY (`cfv_id`),
  KEY `cfv_content` (`cfv_content`),
  KEY `cfv_field` (`cfv_field`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',
	
	// CONTENT INTERACTIONS
	'cn_content_interactions' =>

'CREATE TABLE `cn_content_interactions` (
  `ci_id` int(5) NOT NULL AUTO_INCREMENT,
  `ci_content_type` int(3) NOT NULL,
  `ci_name` varchar(50) DEFAULT NULL,
  `ci_on_text` varchar(100) DEFAULT NULL,
  `ci_off_text` varchar(100) DEFAULT NULL,
  `ci_on_alert` varchar(200) DEFAULT NULL,
  `ci_off_alert` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`ci_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',

	// CONTENT-CONTENT RELATION TYPES
	'cn_content_relation_types' =>

'CREATE TABLE `cn_content_relation_types` (
  `crt_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `crt_type1` bigint(20) DEFAULT NULL,
  `crt_type2` bigint(20) DEFAULT NULL,
  `crt_type3` bigint(20) DEFAULT NULL,
  `crt_relation_type` varchar(20) DEFAULT NULL,
  `crt_name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`crt_id`),
  KEY `crt_type1` (`crt_type1`),
  KEY `crt_type2` (`crt_type2`),
  KEY `crt_type3` (`crt_type3`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',
	
	// CONTENT-TAG RELATIONS
	'cn_content_Tag_relations' =>
	
'CREATE TABLE `cn_content_tag_relations` (
  `ctr_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `ctr_content` bigint(20) NOT NULL,
  `ctr_tag` int(11) NOT NULL,
  PRIMARY KEY (`ctr_id`),
  KEY `ctr_listing` (`ctr_content`),
  KEY `ctr_tag` (`ctr_tag`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;',
	
	// CONTENT TYPES
	'cn_content_types' =>
	
'CREATE TABLE `cn_content_types` (
  `ct_id` int(3) NOT NULL AUTO_INCREMENT,
  `ct_permalink` varchar(100) DEFAULT NULL,
  `ct_title` varchar(100) DEFAULT NULL,
  `ct_plural` varchar(100) DEFAULT NULL,
  `ct_search_appear` tinyint(1) DEFAULT \'0\',
  `ct_order_column` varchar(30) DEFAULT NULL,
  `ct_order_direction` varchar(4) DEFAULT NULL,
  PRIMARY KEY (`ct_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',

	// FEEDBACK
	'cn_feedback' =>
	
'CREATE TABLE `cn_feedback` (
  `feedback_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `feedback_user` bigint(20) NOT NULL,
  `feedback_content` bigint(20) NOT NULL,
  `feedback_display_name` varchar(100) DEFAULT NULL,
  `feedback_comment` longtext,
  `feedback_rating` int(3) DEFAULT NULL,
  `feedback_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `feedback_ip` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`feedback_id`),
  KEY `feedback_content` (`feedback_content`),
  KEY `feedback_user` (`feedback_user`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;',

	// META FOR CONTENT FIELDS
	'cn_field_meta' =>

'CREATE TABLE `cn_field_meta` (
  `cfm_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `cfm_field` bigint(20) NOT NULL,
  `cfm_key` varchar(50) DEFAULT NULL,
  `cfm_value` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`cfm_id`),
  KEY `cfm_field` (`cfm_field`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',

	// NAV MENUS
	'cn_nav_menus' =>
	
'CREATE TABLE `cn_nav_menus` (
  `nav_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `nav_resource` bigint(20) DEFAULT NULL,
  `nav_resource_type` varchar(50) DEFAULT NULL,
  `nav_menu` varchar(50) DEFAULT NULL,
  `nav_depth` tinyint(3) DEFAULT NULL,
  `nav_order` int(6) DEFAULT NULL,
  `nav_parent` int(11) DEFAULT NULL,
  `nav_display_name` varchar(200) DEFAULT NULL,
  PRIMARY KEY (`nav_id`),
  KEY `nav_resource` (`nav_resource`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',
	
	// OPTIONS
	'cn_options' =>
	
'CREATE TABLE `cn_options` (
  `option_id` int(11) NOT NULL AUTO_INCREMENT,
  `option_name` varchar(50) NOT NULL,
  `option_value` varchar(500) NOT NULL,
  `option_alt_value` varchar(500) DEFAULT NULL,
  PRIMARY KEY (`option_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',
	
	// PAGE VIEW STATS
	'cn_pageviews' =>
	
'CREATE TABLE `cn_pageviews` (
  `pv_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `pv_ip` varchar(20) DEFAULT NULL,
  `pv_page` varchar(200) DEFAULT NULL,
  `pv_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `pv_type` varchar(20) DEFAULT NULL,
  `pv_error` int(3) DEFAULT NULL,
  PRIMARY KEY (`pv_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;',
	
	// TAGS
	'cn_tags' =>
	
'CREATE TABLE `cn_tags` (
  `tag_id` int(11) NOT NULL AUTO_INCREMENT,
  `tag_title` varchar(50) NOT NULL,
  `tag_permalink` varchar(100) NOT NULL,
  `tag_group` int(3) NOT NULL,
  `tag_content_type` int(3) NOT NULL,
  `tag_type` varchar(20) NOT NULL,
  `tag_order` varchar(50) NOT NULL,
  PRIMARY KEY (`tag_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;',

	// UPLOADS
	'cn_uploads' =>

'CREATE TABLE `cn_uploads` (
  `upload_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `upload_field` bigint(20) NOT NULL,
  `upload_content` bigint(20) DEFAULT NULL,
  `upload_filename` varchar(100) DEFAULT NULL,
  `upload_order` int(11) DEFAULT NULL,
  PRIMARY KEY (`upload_id`),
  KEY `upload_content` (`upload_content`),
  KEY `upload_field` (`upload_field`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',
	
	// USERS
	'cn_users' =>

'CREATE TABLE `cn_users` (
  `user_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_password` varchar(50) NOT NULL,
  `user_email` varchar(50) DEFAULT NULL,
  `user_level` int(1) DEFAULT NULL,
  `user_username` varchar(100) DEFAULT NULL,
  `user_first_name` varchar(50) DEFAULT NULL,
  `user_last_name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;',

	// USER CONTENT RELATIONS
	'cn_user_content_relations' =>
	
'CREATE TABLE `cn_user_content_relations` (
  `ucr_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `ucr_content` bigint(20) NOT NULL,
  `ucr_user` bigint(20) NOT NULL,
  `ucr_interaction` int(5) NOT NULL,
  `ucr_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`ucr_id`),
  KEY `ucr_content` (`ucr_content`),
  KEY `ucr_user` (`ucr_user`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;',

	// USER GROUPS
	'cn_user_groups' =>

'CREATE TABLE `cn_user_groups` (
  `group_id` int(5) NOT NULL AUTO_INCREMENT,
  `group_name` varchar(50) NOT NULL,
  PRIMARY KEY (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;',

	// USER USER RELATIONS
	'cn_user_user_relations' =>

'CREATE TABLE `cn_user_user_relations` (
  `uur_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `uur_from` bigint(20) NOT NULL,
  `uur_to` bigint(20) NOT NULL,
  `uur_type` int(3) NOT NULL,
  `uur_message` varchar(500) DEFAULT NULL,
  `uur_resource` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`uur_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
'

	
);


$cn_db_default_fields = array(array('ID', 'id', 'Automatic', '1', '1'), array('Title', 'title', 'ShortText', '1', '1'), array('Permalink', 'permalink', 'Automatic', '0', '1'), array('Content', 'content', 'LongText', '0', '0'), array('Author', 'author', 'Automatic', '0', '1'), array('Status', 'status', 'Status', '0', '1'), array('Timestamp', 'timestamp', 'Automatic', '0', '1'), array('Parent Content', 'parent', 'ParentContent', '0', '0'), array('Tags', 'tags', 'Tags', '0', '0'));

$cn_db_default_fieldnames = array('Title', 'ID', 'Permalink', 'Content', 'Author', 'Status', 'Timestamp', 'Parent', 'Tags');

$cn_db_users_default_fields = array(array('ID', 'id', 'ID'), array('E-Mail', 'email', 'Email'), array('Password', 'password', 'Password'), array('Level', 'level', 'Level'), array('Username', 'username', 'ShortText'));

$cn_db_users_default_fieldnames = array('ID', 'E-Mail', 'Password', 'Level', 'Username');



function insertDefaultFields() {
	global $cn_db_default_fields;
	global $cn_db_users_default_fields;
	/** TRUNCATE FIELDS TABLE (to prevent duplication) 
	mysql_query("TRUNCATE TABLE `cn_content_fields`"); */
	
	/** Add fields */
	foreach($cn_db_default_fields as $data) {
		$name = $data[0];
		$type = $data[2];
		$show = $data[3];
		for($i=1;$i<=5;$i++) {
			
			$column = 'l'.$i.'_'.$data[1];
			
			if($i == 1) {
				if($name !='Parent') {
					mysql_query("INSERT INTO `cn_content_fields` (`cf_display_name`, `cf_column`, `cf_required`, `cf_level`, `cf_data_type`, `cf_show`) VALUES('$name', '$column', '1', '1', '$type', '$show')");
				}
			}
			else {
				mysql_query("INSERT INTO `cn_content_fields` (`cf_display_name`, `cf_column`, `cf_required`, `cf_level`, `cf_data_type`, `cf_show`) VALUES('$name', '$column', '1', '$i', '$type', '$show')");
			}
		}
	}
	
	/** Add users fields */
	foreach($cn_db_users_default_fields as $data) {
		$name = $data[0];
		$type = $data[2];
		$column = 'user_'.$data[1];
		
		if($column == 'user_email') {
			mysql_query("INSERT INTO `cn_content_fields` (`cf_display_name`, `cf_column`, `cf_required`, `cf_content_type`, `cf_data_type`, `cf_show_login`, `cf_show_signup`) VALUES('$name', '$column', '1', '0', '$type', '1', '1')");
		}
		elseif($column == 'user_password') {
			mysql_query("INSERT INTO `cn_content_fields` (`cf_display_name`, `cf_column`, `cf_required`, `cf_content_type`, `cf_data_type`, `cf_show_login`, `cf_show_edit_profile`) VALUES('$name', '$column', '1', '0', '$type', '1', '1')");
		}
		else {
			mysql_query("INSERT INTO `cn_content_fields` (`cf_display_name`, `cf_column`, `cf_required`, `cf_content_type`, `cf_data_type`) VALUES('$name', '$column', '1', '0', '$type')");
		}
	}
}					
						

?>